﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnForum.Models;
using PagedList;

namespace DoAnForum.Controllers
{
    public class TopicController : Controller
    {
        private static DoAnForumEntities db = new DoAnForumEntities();
        //
        // GET: /Topic/
        [HttpGet]
        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult CreateTopic(int? SelectedId)
        {
            var query = db.Categories.Where(x => x.Level != 1).Select(c => new { c.CategoriId, c.CategoryName });
            if (SelectedId!=null)
            {
                ViewBag.Category = new SelectList(query.AsEnumerable(), "CategoriId", "CategoryName", SelectedId);
            }
            ViewBag.Category = new SelectList(query.AsEnumerable(), "CategoriId", "CategoryName");
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateTopic(CreateTopicViewModel model)
        {
            if (ModelState.IsValid)
            {
                var query = db.Categories.Select(c => new { c.CategoriId, c.CategoryName });
                ViewBag.Category = new SelectList(query.AsEnumerable(), "CategoriId", "CategoryName");
                var comment = new Comment();
                var topic = new Topic();
                var tag = new Tag();
                var currUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == User.Identity.Name);

                //Add New Topic
                topic.TopicTitle = model.TopicTitle;
                topic.TopicShortName = model.TopicTitle;
                topic.TopicDescription = model.TopicTitle;
                topic.TopicCreationDate = DateTime.Today;
                topic.TopicLastEditDate = DateTime.Today;
                topic.TopicViews = 0;
                topic.CategoriId = model.Category;
                topic.Active = topic.TopicIsClose = true;
                if (currUser != null)
                {
                    topic.UserId = currUser.UserId;
                    topic.TopicLastEditUser = currUser.UserId;
                }
                db.Topics.AddObject(topic);
                db.SaveChanges();

                //Get New Topic
                var newTopic =
                    db.Topics.Where(x => x.TopicTitle == model.TopicTitle).OrderBy(x => x.TopicCreationDate).
                        FirstOrDefault();

                //Add New Comment
                if (newTopic != null) comment.TopicId = newTopic.TopicId;
                comment.CommentBody = model.TopicContent;
                comment.CommentCreationDate = DateTime.Today;
                comment.CommentLastEditDate = DateTime.Today;
                comment.IsStartTopic = true;
                comment.Active = true;
                if (currUser != null)
                {
                    comment.UserId = currUser.UserId;
                    comment.CommentLastEditUser = currUser.UserId;
                }

                db.Comments.AddObject(comment);
                db.SaveChanges();

                //add Tag
                string[] s = model.Tags.Split(',');
                for (int i = 0; i < s.Length; i++)
                {
                    string si = s[i];
                    var tagvalue = db.Tags.SingleOrDefault(x => si.Equals(x.Tag1));
                    if (tagvalue != null) newTopic.Tags.Add(tagvalue);
                    else
                    {
                        var temp = new Tag();
                        temp.Tag1 = s[i];
                        db.Tags.AddObject(temp);
                        db.SaveChanges();
                        newTopic.Tags.Add(temp);
                        db.SaveChanges();
                    }
                }
            }
            return View();
        }

        public ActionResult ShowTopic(int id)
        {
            ViewBag.TopicId = id;
            return View();
        }

        public ActionResult ShowTopicPartial(int id, int? page)
        {
            int pageNumber = (page ?? 1);
            var temp = db.Topics.SingleOrDefault(x => x.TopicId == id);
            ViewBag.TopicTitle = temp.TopicTitle;
            var cm = db.Comments.Where(x => x.TopicId == id).OrderBy(x => x.CommentCreationDate).ToPagedList(pageNumber, 10);
            ViewBag.TopicId = id;
            temp.TopicViews += 1;
            db.SaveChanges();
            return PartialView("ShowTopicPartial",cm);
        }
    }
}
